if(module) module.exports = json;
export default function json (stream:any,MAX = 1024*1024*1024){
    let par:any = {parameter:{},state:true};
    return new Promise((reserved,error)=>{
        let size = 0;
        let data = [];
        let start = (a:Buffer)=>{
            data.push(a);
            size += a.length;
            if(size>MAX){
                stream.removeListener("data",start);
                stream.removeListener("end",end);
                error(new Error("数据量超界"));
            }
        }
        let end = ()=>{
            try{
                par.parameter = JSON.parse(Buffer.concat(data).toString());
            }catch(e){
                //到这里就有可能是字符串或者数字，给个默认的对象名
                par.parameter = {json:Buffer.concat(data).toString()};
            }
            reserved(par);
        }

        stream.on("data",start)
        stream.on("end",end)
        stream.on("error",(err:Error)=>{console.log(err.message)})
    })
}
